// 初始化地图
var map;
// 初始化控件
var navigationControl;
var scaleControl;
var geolocationControl;
var routeControl;
var markerControl;
var cleanControl;
// 初始化服务
var geolocationService;
var transitService;
var drivingService;
var walkingService;
// 初始化视图
var genMarker;
var targetMarker;
/**
 * 基础地图
 */ 
var createMap=function (layoutId) {
	map = new BMap.Map("map"/*, {mapType:BMAP_PERSPECTIVE_MAP}*/);// 以3d形式展示
	var point = new BMap.Point(116.4035,39.915);
	map.setCurrentCity("上海");// 选择城市来加载3d
	map.centerAndZoom("上海", 19);
	// 添加缩放与方向键
	navigationControl=new BMap.NavigationControl();
	map.addControl(navigationControl);
	// 添加地图尺码
	scaleControl=new BMap.ScaleControl();
	map.addControl(scaleControl);
	// 加载定位控件
	geolocationControl=new GeolocationControl();
	map.addControl(geolocationControl);
	// 加载路线查询
	routeControl=new RouteControl();
	map.addControl(routeControl);
	//   加载标记工具
	markerControl=new MarkerControl();
	map.addControl(markerControl);
	//  加载标记工具
	cleanControl=new CleanControl();
	map.addControl(cleanControl);
	// 获取定位
	GeolocationControl.showCurrentPosition();
	
	return map;
};
/**
 * 定位控件
 */ 
var GeolocationControl=function () {// 构造函数
	this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;  
    this.defaultOffset = new BMap.Size(10, 216);
};
GeolocationControl.prototype = new BMap.Control();// 继承
GeolocationControl.prototype.initialize = function(map) {// 覆盖initialize方法
	GeolocationControl.map=map;// 保存map
	var button = document.createElement("button");
	button.innerHTML="定位";
	button.style.cssText="width:44px;height:44px;";
	button.onclick = GeolocationControl.showCurrentPosition;
	map.getContainer().appendChild(button);
	return button;
};
GeolocationControl.showCurrentPosition = function() {
	/* 
	 * 在3秒钟内出现两个定位在0.001个纬度之内时认定为找到定位，
	 * 如果两个位置跨仍非常大则继续定位，
	 * 如果超过3秒则定位在最后一次
	 */
	var map = GeolocationControl.map;
	if (!map) {
		alert("请先加载GeolocationControl到地图中");
	}
	if (!geolocationService) {
		geolocationService = new BMap.Geolocation();
	}
	geolocationService.getCurrentPosition(function(result){
	    if(this.getStatus() == BMAP_STATUS_SUCCESS){
	    	if (!genMarker) {
	    		genMarker = new BMap.Marker();
	    	}
	    	genMarker.setPosition(result.point);
	    	genMarker.disableMassClear();
	        map.addOverlay(genMarker);
	        map.panTo(genMarker.getPosition());
	    }
	    else {
	        alert('failed'+this.getStatus());
	    }
	});
};
/**
 * 路线查询控件
 */
var RouteControl=function () {
	this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;  
    this.defaultOffset = new BMap.Size(10, 263);
};
RouteControl.prototype = new BMap.Control();
RouteControl.prototype.initialize=function (map) {
	RouteControl.map=map;
	var container=document.createElement("div");
	container.style.cssText="height:44px;";
	// 步行
	var walkingButton=document.createElement("button");
	walkingButton.innerHTML="步行";
	walkingButton.style.cssText="width:44px;height:44px;";
	walkingButton.onclick=RouteControl.showWalkingRoute;
	container.appendChild(walkingButton);
	// 公交
	var transitButton=document.createElement("button");
	transitButton.innerHTML="公交";
	transitButton.style.cssText="width:44px;height:44px;";
	transitButton.onclick=RouteControl.showTransitRoute;
	container.appendChild(transitButton);
	// 驾车
	var drivingButton=document.createElement("button");
	drivingButton.innerHTML="驾车";
	drivingButton.style.cssText="width:44px;height:44px;";
	drivingButton.onclick=RouteControl.showDrivingRoute;
	container.appendChild(drivingButton);
	
	map.getContainer().appendChild(container);
	return container;
};
RouteControl.showTransitRoute=function () {
	if (!genMarker || !targetMarker) {
		alert('请指出一个目标');
		return;
	}
	var map=RouteControl.map;
	if (!map) {
		alert("请先加载RouteControl到地图中");
	}
	transitService = new BMap.TransitRoute(map, {
		renderOptions: {map: map, autoViewport: true}
	});
	transitService.search(genMarker.getPosition(), targetMarker.getPosition());
};
RouteControl.showDrivingRoute=function () {
	if (!genMarker || !targetMarker) {
		alert('请指出一个目标');
		return;
	}
	var map=RouteControl.map;
	if (!map) {
		alert("请先加载RouteControl到地图中");
	}
	drivingService = new BMap.DrivingRoute(map, {
		renderOptions:{map: map, autoViewport: true}}
	);
	drivingService.search(genMarker.getPosition(), targetMarker.getPosition());
};
RouteControl.showWalkingRoute=function () {
	if (!genMarker || !targetMarker) {
		alert('请指出一个目标');
		return;
	}
	var map=RouteControl.map;
	if (!map) {
		alert("请先加载RouteControl到地图中");
	}
	walkingService = new BMap.WalkingRoute(map, {
		renderOptions:{map: map, autoViewport: true}
	});
	walkingService.search(genMarker.getPosition(), targetMarker.getPosition());
};
/**
 * 放置目标控件
 */
var MarkerControl=function () {
	this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;  
    this.defaultOffset = new BMap.Size(10, 310);
};
MarkerControl.prototype = new BMap.Control();
MarkerControl.prototype.initialize=function (map) {
	MarkerControl.map=map;
	var button = document.createElement("button");
	button.innerHTML="放置目标";
	button.style.cssText="width:44px;height:44px;";
	button.onclick = MarkerControl.showMarker;
	map.getContainer().appendChild(button);
	return button;
};
MarkerControl.showMarker=function (){
	var map=MarkerControl.map;
	if (!map) {
		alert("请先加载MarkerControl到地图中");
	}
	if (!MarkerControl.marker) {
		targetMarker=new BMap.Marker(map.getCenter());
		MarkerControl.marker=targetMarker;
		MarkerControl.marker.enableDragging();
		MarkerControl.marker.disableMassClear();
		map.addOverlay(MarkerControl.marker);
	} else {
		MarkerControl.marker.setPosition(map.getCenter());
	}
};
/**
 * 清除
 */
var CleanControl=function () {
	this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;  
    this.defaultOffset = new BMap.Size(10, 356);
};
CleanControl.prototype = new BMap.Control();
CleanControl.prototype.initialize=function (map) {
	CleanControl.map=map;
	var button = document.createElement("button");
	button.innerHTML="清除";
	button.style.cssText="width:44px;height:44px;";
	button.onclick = CleanControl.clean;
	map.getContainer().appendChild(button);
	return button;
};
CleanControl.clean=function(){
	var map=CleanControl.map;
	if (!map) {
		alert("请先加载CleanControl到地图中");
	}
	map.clearOverlays();
};
